// 每个状态管理文件都要引入此方法
import { defineStore } from 'pinia'
import router from "@/router";
import { ElNotification  } from 'element-plus'
import { getNowTime } from "@/utils/time"

import i18n from "@/lang/i18n";
import notify from "@/utils/notify";



export const usePiniaStore = defineStore('pinia',{
    state:()=>{
        return{
            isLogin : false,
            num:-10
        }
    },
    //state也可写成这样
    // state:()=>({
    //     num:0
    // }),
    actions:{
        changeNum(num:number){
            this.num += 1
            //这里可以使用this去拿到state里定义的变量
            console.log("this.num",this.num)
            console.log("num",num)

        },
        login(){
            this.isLogin = true
            localStorage.setItem('isLogin','true')
            console.log('登录啦…')
            router.push("/")
            ElNotification({
                title: i18n.global.t("loginSuccessed"),
                message: getNowTime(),
                type: 'success',
            })
        },
        logout(){
            this.isLogin = false
            localStorage.setItem('isLogin',"false")
            console.log('退出啦…')
            router.push("/login")
            notify({
                title : i18n.global.t("logout"),
                body : i18n.global.t("logoutSuccessed")
            })
            ElNotification({
                title: i18n.global.t("logoutSuccessed"),
                message: getNowTime(),
                type: 'info',
            })
        },

    },
    getters:{
        // 这里取名不可与state里的变量一致 所以取名getNum
        getNum:state=>state.num
    }
})